//
//  NSECGStandardView.m
//  SpeedTest
//
//  Created by Han Mingjie on 2020/2/10.
//  Copyright © 2020 MingJie Han. All rights reserved.
//

#import "NSECGStandardView.h"

@interface NSECGStandardView(){
    
}
@end

@implementation NSECGStandardView
@synthesize y_rate,line_color,line_width;
@synthesize pix_per_mm,ecg_name_array;
@synthesize standard_text_color;
@synthesize standard_text_font;
@synthesize standard_style;
@synthesize style;

-(void)setRefresh{
#if TARGET_OS_IOS
    self.backgroundColor = [UIColor clearColor];
    [self setNeedsDisplay];
#else
    [self setNeedsDisplay:YES];
#endif
}

-(void)setY_rate:(float)_y_rate{
    y_rate = _y_rate;
    [self setRefresh];
}

-(void)setEcg_name_array:(NSArray *)_ecg_name_array{
    ecg_name_array = _ecg_name_array;
    [self setRefresh];
}

-(void)setPix_per_mm:(float)_pix_per_mm{
    pix_per_mm = _pix_per_mm;
    [self setRefresh];
}

-(void)setLine_color:(UIColor *)_line_color{
    line_color = _line_color;
    [self setRefresh];
}

-(void)setStandard_text_color:(UIColor *)_standard_text_color{
    standard_text_color = _standard_text_color;
}

-(void)setStandard_text_font:(UIFont *)_standard_text_font{
    standard_text_font = _standard_text_font;
}

-(void)setLine_width:(float)_line_width{
    line_width = _line_width;
    [self setRefresh];
}

-(float)middleFor:(NSString *)lead_name{
    float per_line_height = self.frame.size.height/(ecg_name_array.count+1);
    NSUInteger index = [ecg_name_array indexOfObject:lead_name] + 1;
#if TARGET_OS_IOS
    return index * per_line_height;
#else
    return self.frame.size.height - index * per_line_height;
#endif
}

#if !TARGET_OS_IOS
-(void)setFrameSize:(NSSize)newSize{
    [super setFrameSize:newSize];
    [self needsDisplay];
}
#endif
    
-(void)draw_line{
    CGMutablePathRef standard_line_path = CGPathCreateMutable();
    BOOL cancelOddStandard = NO;
    BOOL drawStandard = NO;
    switch (standard_style) {
        case NSECGRealTimeView_Standard_Show:
            drawStandard = YES;
            break;
        case NSECGRealTimeView_Standard_Hidden:
            drawStandard = NO;
            break;
        case NSECGRealTimeView_Standard_Auto:
            switch (style) {
                case NSECGRealTimeView_Style_6x2:
                    drawStandard = YES;
                    break;
                case NSECGRealTimeView_Style_limb_leads:
                    drawStandard = YES;
                    break;
                case NSECGRealTimeView_Style_3x4:
                    drawStandard = NO;
                    if (1 == ecg_name_array.count){
                        drawStandard = YES;
                    }
                    break;
                case NSECGRealTimeView_Style_12x1:
                    if (self.frame.size.height/pix_per_mm < (y_rate + 2) * (ecg_name_array.count +1)){
                        cancelOddStandard = YES;
                    }
                    drawStandard = YES;
                    break;
                default:
                    break;
            }
            break;
        default:
            break;
    }
    
    for (int i=0;i<ecg_name_array.count;i++){
        NSString *lead_name = [ecg_name_array objectAtIndex:i];
        float middle = [self middleFor:lead_name];
        
        if (cancelOddStandard && 0 == i%2){
            continue;
        }
        
        //draw lead name
        #if TARGET_OS_IOS
                CGRect rect = CGRectMake(0.f, middle + 5.f, 5 * pix_per_mm, 18.f);
        #else
                CGRect rect = CGRectMake(0.f, middle-24.f, 5 * pix_per_mm, 18.f);
        #endif
                NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
                paragraphStyle.lineBreakMode = NSLineBreakByClipping;
                paragraphStyle.alignment = NSTextAlignmentCenter;
                NSDictionary *attributes = @{NSFontAttributeName:standard_text_font,
                                             NSForegroundColorAttributeName:standard_text_color,
                                             NSParagraphStyleAttributeName:paragraphStyle};
        #if TARGET_OS_IOS
                [lead_name drawInRect:rect withAttributes:attributes];
        #else
                [lead_name drawInRect:NSRectFromCGRect(rect) withAttributes:attributes];
        #endif
        
        //build standard path
        CGPathMoveToPoint(standard_line_path, nil, 0.f, middle);
        CGPathAddLineToPoint(standard_line_path, nil, 1.5 * pix_per_mm, middle);
#if TARGET_OS_IOS
        CGPathAddLineToPoint(standard_line_path, nil, 1.5 * pix_per_mm, middle - y_rate * pix_per_mm);
        CGPathAddLineToPoint(standard_line_path, nil, 3.5 * pix_per_mm, middle - y_rate * pix_per_mm);
#else
        CGPathAddLineToPoint(standard_line_path, nil, 1.5 * pix_per_mm, middle + y_rate * pix_per_mm);
        CGPathAddLineToPoint(standard_line_path, nil, 3.5 * pix_per_mm, middle + y_rate * pix_per_mm);
#endif
        CGPathAddLineToPoint(standard_line_path, nil, 3.5 * pix_per_mm, middle);
        CGPathAddLineToPoint(standard_line_path, nil, 5 * pix_per_mm, middle);
        CGPathMoveToPoint(standard_line_path, NULL, -1.f, -1.f);
        CGPathCloseSubpath(standard_line_path);
    }
    
#if TARGET_OS_IOS
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
#else
    CGContextRef currentContext = [NSGraphicsContext currentContext].CGContext;
#endif
    
    if (drawStandard){
        CGContextSetLineWidth(currentContext, line_width);
        CGContextSetStrokeColorWithColor(currentContext, line_color.CGColor);
        CGContextSetLineCap(currentContext, kCGLineCapRound);
        CGContextSetLineJoin(currentContext, kCGLineJoinRound);
        CGContextBeginPath(currentContext);
        CGContextAddPath(currentContext, standard_line_path);
        CGContextStrokePath(currentContext);
    }
    CGPathRelease(standard_line_path);
}
    
-(void)drawRect:(CGRect)dirtyRect{
    [super drawRect:dirtyRect];
    if (0 >= line_width){
        return;
    }
    if (nil == line_color){
        return;
    }
    if (pix_per_mm <= 0){
        return;
    }
    if (nil == ecg_name_array){
        return;
    }
    if (y_rate <= 0.f){
        return;
    }
    if (nil == standard_text_font){
        return;
    }
    [self draw_line];
}

@end


